package com.tsd.jxc.req;

import com.alibaba.fastjson.JSON;
import com.tsd.core.datasource.SpringContextUtil;
import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.jxc.JxcConstants;
import com.tsd.jxc.goods.dao.JxcGoodsMapper;
import com.tsd.jxc.goods.vo.JxcGoodsVo;
import com.tsd.jxc.sale.entity.JxcSaleOrderDetail;
import com.tsd.jxc.sale.entity.JxcSaleOrderDetailExt;
import com.tsd.jxc.utils.ClothingUtil;
import com.tsd.jxc.vo.JxcSettingVo;
import com.tsd.utils.AutoInjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @ClassName: JxcSaleOrderDetailReqParamsVo
 * @Description: TODO
 * @Author: Hillpool
 * @Date: 2023/3/13 14:09
 * @Version: 1.0
 */
public class JxcSaleOrderDetailReqParamsVo extends JxcSaleOrderDetail {

    private Map<String, BigDecimal> preset_qty_map;

    private Map<String, String> preset_product_map;

    private Map<String, Long> preset_size_id_map;

    private String goods_name;

    private String goods_unit;

    private String specifications;

    private String spec_sid;

    private String spec_preset_value;

    public Map<String, BigDecimal> getPreset_qty_map() {
        return preset_qty_map;
    }

    public void setPreset_qty_map(Map<String, BigDecimal> preset_qty_map) {
        this.preset_qty_map = preset_qty_map;
    }

    public Map<String, String> getPreset_product_map() {
        return preset_product_map;
    }

    public void setPreset_product_map(Map<String, String> preset_product_map) {
        this.preset_product_map = preset_product_map;
    }

    public Map<String, Long> getPreset_size_id_map() {
        return preset_size_id_map;
    }

    public void setPreset_size_id_map(Map<String, Long> preset_size_id_map) {
        this.preset_size_id_map = preset_size_id_map;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public String getGoods_unit() {
        return goods_unit;
    }

    public void setGoods_unit(String goods_unit) {
        this.goods_unit = goods_unit;
    }

    public String getSpecifications() {
        return specifications;
    }

    public void setSpecifications(String specifications) {
        this.specifications = specifications;
    }

    public String getSpec_sid() {
        return spec_sid;
    }

    public void setSpec_sid(String spec_sid) {
        this.spec_sid = spec_sid;
    }

    public String getSpec_preset_value() {
        return spec_preset_value;
    }

    public void setSpec_preset_value(String spec_preset_value) {
        this.spec_preset_value = spec_preset_value;
    }

    public String getSpec_name() {
        return null;
    }

    public static List<JxcBuyOrderDetailReqParamsVo> transfer(List<JxcSaleOrderDetailExt> detailExtList, JxcSettingVo settingVo) {
        if (HlpUtils.isEmptyList(detailExtList)) {
            return null;
        }
        List<JxcBuyOrderDetailReqParamsVo> voList = new ArrayList<>();
        if (!HlpUtils.isEmptyList(detailExtList)) {
            List<String> sizeList = ClothingUtil.getClothing_preset_size_list(settingVo);
            JxcGoodsMapper jxcGoodsMapper = SpringContextUtil.getBean(JxcGoodsMapper.class);
            Map<String, Object> params = new HashMap<>();
            params.put("goods_list", ListUtil.map(detailExtList, JxcSaleOrderDetailExt::getGoods_sid));
            List<JxcGoodsVo> goodsVoList = jxcGoodsMapper.select4ClothingByPage(params, null);
            for (JxcGoodsVo goodsVo : goodsVoList) {
                List<JxcSaleOrderDetailExt> filterDetailList = null;//ListUtil.filter(detailExtList, t -> goodsVo.containsProduct(t.getProduct_sid()));
                if (HlpUtils.isEmptyList(filterDetailList)) {
                    continue;
                }
                JxcBuyOrderDetailReqParamsVo detailReqParamsVo = JSON.parseObject(JSON.toJSONString(filterDetailList.get(0)), JxcBuyOrderDetailReqParamsVo.class);
                AutoInjectUtils.clearBaseData(detailReqParamsVo);
                detailReqParamsVo.setSn(filterDetailList.get(0).getSn());
//                detailReqParamsVo.setSpec_preset_value(goodsVo.getSpec_preset_value());
//                detailReqParamsVo.setPreset_product_map(goodsVo.getClothing_preset_product_map());
//                Map<String, String> presetProductMap = goodsVo.getClothing_preset_product_map();
                Map<String, BigDecimal> presetQtyMap = new HashMap<>();
                Map<String, Long> presetSizeIdMap = new HashMap<>();
                Map<String, String> presetSaleMap = new HashMap<>();
                int index = 0;
                for (String size : sizeList) {
                    String productSid = null;//presetProductMap.get(size);
                    if (HlpUtils.isEmpty(productSid)) {
                        index += 1;
                        continue;
                    }
                    JxcSaleOrderDetailExt detailExt = null;//ListUtil.get(filterDetailList, t -> productSid.equals(t.getProduct_sid()));
                    if (detailExt == null) {
                        index += 1;
                        continue;
                    }
                    presetQtyMap.put("preset_qty_" + index, detailExt.getQty());
                    presetSizeIdMap.put(size, detailExt.getId());
                    presetSaleMap.put(size, String.format("%s_%s", detailExt.getOrder_sid(), detailExt.getSid()));
                    index += 1;
                }
                detailReqParamsVo.setPreset_qty_map(presetQtyMap);
                detailReqParamsVo.setPreset_sale_map(presetSaleMap);
                detailReqParamsVo.setPreset_size_id_map(presetSizeIdMap);
                double totalQty = 0D, totalAmount = 0D, totalTaxAmount = 0D, totalStockQty = 0D,
                        totalGenBillNum = 0D, totalGenBuyQty = 0D;
                for (JxcSaleOrderDetailExt detailExt : filterDetailList) {
                    totalQty = DoubleUtil.add(totalQty, detailExt.getQty());
                    totalAmount = DoubleUtil.add(totalAmount, detailExt.getAmount());
                    totalTaxAmount = DoubleUtil.add(totalTaxAmount, detailExt.getTax_amount());
                    totalStockQty = DoubleUtil.add(totalStockQty, detailExt.getStock_qty());
                    totalGenBillNum = DoubleUtil.add(totalGenBillNum, detailExt.getGen_bill_num());
                    totalGenBuyQty = DoubleUtil.add(totalGenBuyQty, detailExt.getGen_buy_qty());
                }
                detailReqParamsVo.setQty(BigDecimal.valueOf(totalQty));
                detailReqParamsVo.setAmount(BigDecimal.valueOf(totalAmount));
                detailReqParamsVo.setTax_amount(BigDecimal.valueOf(totalTaxAmount));
//                detailReqParamsVo.setStock_qty(BigDecimal.valueOf(totalStockQty));
                detailReqParamsVo.setGen_bill_num(BigDecimal.valueOf(totalGenBillNum).intValue());
                detailReqParamsVo.setGen_buy_qty(BigDecimal.valueOf(totalGenBuyQty));
                voList.add(detailReqParamsVo);
            }
        }
        voList = ListUtil.sorted(voList, Comparator.comparing(JxcBuyOrderDetailReqParamsVo::getSn));
        return voList;
    }
}
